/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.cggveritas.crewintranet.jms.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Alassane
 */
public class Helpers {

    /**
     * Cette fonction verifie si un numero de téléphone est valide
     *
     * @param monNumero
     * @return boolean
     */
    public static boolean monNumeroEstValide(String monNumero) {
        Pattern pattern = Pattern.compile("\\+?\\d{0,4}?( |-)?\\d{4,12}");
        Matcher matcher = pattern.matcher(monNumero);
        return matcher.matches();
    }

    /**
     *
     * @param email
     * @return
     */
    public static boolean valideEmail(String email) {

        return email.matches("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
    }

    /**
     *
     * @param stringText
     * @param motifOfBreak
     * @return
     */
    public static List<String> converterStringToListeString(String stringText, String motifOfBreak) {
        List<String> resultat = new ArrayList<String>();
        String[] tab = stringText.split(motifOfBreak);
        if (tab.length > 0) {
            resultat.addAll(Arrays.asList(tab));
        }
        return resultat;
    }

    /**
     *
     * @param listStrings
     * @param motifOfBreak
     * @return
     */
    public static String converterListeStringToString(List<String> listStrings, String motifOfBreak) {
        String resultatString = "";
        if (listStrings.size() > 0) {
            for (String i : listStrings) {
                resultatString += i + ";";
            }
            resultatString = resultatString.substring(0, resultatString.length() - 1);
        }
        return resultatString;
    }
    
     public static Date addDaysToDate(Date date, int nbDays){
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.DATE, nbDays);
        return cal.getTime();
    }
    
     public static String separateur(String chaine, String expression) {
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(chaine);
        if (matcher.find()) {
            return matcher.group(0).toString();
        }
        return "";
    }
}
